<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "user_consumption".
 *
 * @property int $id
 * @property int $uid 用户
 * @property int $order_id 订单
 * @property int $money 价格
 * @property int $type 流动类型
 * @property int $created_at 时间
 *
 * @property Order $order
 * @property User $u
 */
class UserConsumption extends \yii\db\ActiveRecord
{
    const TYPE_WX = 1;


    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'user_consumption';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['uid', 'order_id', 'money', 'type', 'created_at'], 'integer'],
            [['money'], 'require'],
            [['order_id'], 'exist', 'skipOnError' => true, 'targetClass' => Order::className(), 'targetAttribute' => ['order_id' => 'id']],
            [['uid'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['uid' => 'id']],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id'         => 'ID',
            'uid'        => '用户',
            'order_id'   => '订单',
            'money'      => '金额',
            'type'       => '流动类型',
            'created_at' => '时间',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getOrder()
    {
        return $this->hasOne(Order::className(), ['id' => 'order_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getU()
    {
        return $this->hasOne(User::className(), ['id' => 'uid']);
    }
}
